package com.metadata_system.system.response;

import com.alibaba.fastjson.JSON;
import com.metadata_system.common.db.entity.MetadataEdge;
import com.metadata_system.common.db.entity.MetadataNode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;

import java.util.*;

@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class NodeItem {
    private String id;
    private Integer et;
    private Integer metadataId;
    private Integer metadataViewId;
    private String uniqueKey;
    private String type;
    private Object data;
    private Map<String, Object> position;
    private List<Integer> viewIdArr;

    public static NodeItem fromEntity(MetadataNode metadataNode) {
        NodeItem nodeItem = NodeItem.builder()
                .id("n" + metadataNode.getId())
                .et(0)
                .metadataId(metadataNode.getMetadataId())
                .metadataViewId(metadataNode.getMetadataViewId())
                .uniqueKey(metadataNode.getUniqueKey())
                .position(JSON.parseObject(metadataNode.getPosition()))
                .viewIdArr(new ArrayList<>())
                .type("custom")
                .build();

        HashMap<String, Object> data = new HashMap<>();
        data.put("label", metadataNode.getName());
        if(!StringUtils.isBlank(metadataNode.getUniqueKey())){
            data.put("uniqueKey", metadataNode.getUniqueKey());
        }
        data.put("properties", JSON.parseArray(StringUtils.isBlank(metadataNode.getProperties()) ? "[]" : metadataNode.getProperties()));
        nodeItem.setData(data);
        return nodeItem;
    }

    public static List<NodeItem> fromEntity(List<MetadataNode> metadataNodes) {
        List<NodeItem> list = new ArrayList<>();
        for (MetadataNode node : metadataNodes) {
            list.add(fromEntity(node));
        }

        // 统计每个元素-名称对应的视图ID列表
        HashMap<String, List<Integer>> map = new HashMap<>();
        for (NodeItem nodeItem : list) {
            if (StringUtils.isNotBlank(nodeItem.getUniqueKey())) {
                List<Integer> viewIdList = map.get(nodeItem.getUniqueKey());
                if (viewIdList == null) {
                    viewIdList = new ArrayList<>();
                }
                if (!viewIdList.contains(nodeItem.getMetadataViewId())) {
                    viewIdList.add(nodeItem.getMetadataViewId());
                }
                map.put(nodeItem.getUniqueKey(), viewIdList);
            } else {
                nodeItem.setViewIdArr(Arrays.asList(nodeItem.getMetadataViewId()));
            }
        }

        // 设置视图ID数组
        for (NodeItem nodeItem : list) {
            if (StringUtils.isNotBlank(nodeItem.getUniqueKey())) {
                List<Integer> viewIdList = map.get(nodeItem.getUniqueKey());
                nodeItem.setViewIdArr(viewIdList);
            }
        }

        // 克隆节点-去重
        HashMap<String, Boolean> isAddUniqueKeyItem = new HashMap<>();

        List<NodeItem> newList = new ArrayList<>();
        for (NodeItem nodeItem : list) {
            if (StringUtils.isNotBlank(nodeItem.getUniqueKey())) {
                if (!isAddUniqueKeyItem.containsKey(nodeItem.getUniqueKey())) {
                    isAddUniqueKeyItem.put(nodeItem.getUniqueKey(), true);
                    newList.add(nodeItem);
                }
            } else {
                newList.add(nodeItem);
            }
        }

        return newList;
    }

    public static List<NodeItem> addDefault(List<NodeItem> list) {
        List<NodeItem> newList = new ArrayList<>();

        HashMap<String, Object> data = new HashMap<>();
        data.put("label", "默认节点");
        newList.add(NodeItem.builder()
                .id("n0")
                .et(0)
                .metadataId(0)
                .metadataViewId(0)
                .uniqueKey("")
                .data(data)
                .position(JSON.parseObject("{}"))
                .viewIdArr(new ArrayList<>())
                .build());

        newList.addAll(list);
        return newList;
    }


}
